from django.http import HttpResponse
from app.models import Chirping
from django.core import serializers
from django.http import QueryDict
from django.db.models import Count


def list(request):
    query = QueryDict(request.body)
    offset = int(query.get('offset'))
    limit = int(query.get('limit'))
    tmp = Chirping.objects()[offset: offset + limit]

    ret = serializers.serialize('json', queryset=tmp)
    return HttpResponse(ret, content_type="application/json")


def total(request):
    total = Chirping.objects().aggregate(total=Count('id'))['total']
    ret = {}
    ret["total"] = total
    return HttpResponse(ret, content_type="application/json")
